<script setup lang="ts">
import { getCodeAPI, postLoginAPI } from '@/apis/common'
import { useUserStore } from '@/stores/userStore'
import type { PostLoginCodeParams } from '@/types/common'
import { ref } from 'vue'

const userStore = useUserStore()
defineOptions({
  options: {
    styleIsolation: 'shared',
  },
})

const showCountDown = ref(false)
// 获取验证码
const getMobileCode = async () => {
  await formRef.value?.validateField?.('mobile')
  showCountDown.value = true
  const res = await getCodeAPI({
    mobile: formData.value.mobile,
    type: 'login',
  })
  console.log('测试阶段短信验证码',res)
}
// 倒计时结束
const onTimeUp = () => {
  showCountDown.value = false
}

const formData = ref<PostLoginCodeParams>({
  mobile: '13230000026',
  code: '',
})
const isAgree = ref(false)
const formRef = ref<UniHelper.UniFormsInstance>()
// 验证表单数据的规则
const formRules = ref<UniHelper.UniFormsRules>({
  mobile: {
    rules: [
      { required: true, errorMessage: '请填写手机号码' },
      { pattern: /^1[3-9]\d{9}$/, errorMessage: '手机号码格式不正确' },
    ],
  },
  code: {
    rules: [
      { required: true, errorMessage: '请输入验证码' },
      { pattern: /^\d{6}$/, errorMessage: '验证码格式不正确' },
    ],
  },
})
// 登录
const handleLogin = async () => {
  if (!isAgree.value) {
    return uni.showToast({
      title: '请先同意用户协议',
      icon: 'none',
    })
  }
  await formRef.value?.validate?.()
  const res = await postLoginAPI(formData.value)
  console.log('res', res)
  userStore.setToken(res.data.token)
  userStore.setUserInfo(res.data)
   // 成功轻提示并跳转 TabBar 首页
   uni.showToast({ title: '登录成功', icon: 'success' })
   uni.switchTab({ url: '/pages/index/index'})
}
</script>

<template>
  <uni-forms class="login-form" ref="formRef" :model="formData" :rules="formRules">
    <uni-forms-item name="mobile">
      <uni-easyinput
        :maxlength="11"
        v-model="formData.mobile"
        :input-border="false"
        :clearable="false"
        placeholder="请输入手机号"
        placeholder-style="color: #C3C3C5"
      />
    </uni-forms-item>
    <uni-forms-item name="code">
      <uni-easyinput
        :maxlength="6"
        v-model="formData.code"
        :input-border="false"
        :clearable="false"
        placeholder="请输入验证码"
        placeholder-style="color: #C3C3C5"
      />
      <text class="text-button" v-if="!showCountDown" @click="getMobileCode">获取验证码</text>
      <view class="text-button" v-else>
        <uni-countdown
          :show-day="false"
          :show-hour="false"
          :show-minute="false"
          :second="5"
          @timeup="onTimeUp"
        />秒后重新发送
      </view>
    </uni-forms-item>

    <view class="agreement">
      <radio :checked="isAgree" @tap="isAgree = !isAgree" color="#16C2A3" />
      我已同意
      <text class="link">用户协议</text>
      及
      <text class="link">隐私协议</text>
    </view>

    <button class="uni-button" @tap="handleLogin">登 录</button>
    {{ formData }}{{ isAgree }}
  </uni-forms>
</template>

<style lang="scss">
@import './styles.scss';
</style>
